package service

import (
	"chat/global"
	"chat/internal/model"
	"time"
)

func GetFriendIdList(userId int) ([]int, error) {
	var list []int

	err := global.DB.Model(&model.UserFriend{}).Select("friend_id").Where("user_id = ?", userId).Find(&list).Error

	return list, err
}

func GetUserFriendList(userId int) ([]model.UserFriend, error) {
	var list []model.UserFriend

	err := global.DB.Where("user_id = ?", userId).Find(&list).Error

	return list, err
}

func CreateRelation(uid int, fid int) error {
	now := time.Now().Unix()
	uf1, uf2 := model.UserFriend{
		CreatedAt: now,
		UserId:    uid,
		FriendId:  fid,
	}, model.UserFriend{
		CreatedAt: now,
		UserId:    fid,
		FriendId:  uid,
	}

	err := global.DB.Create(&uf1).Error
	err = global.DB.Create(&uf2).Error
	return err
}
